﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;

namespace BLL
{
    /// <summary>
    /// The class is used to store a customer's shopping cart.
    /// </summary>
    [Serializable]
    public class Cart
    {
        //Internal storage for a cart
        private Dictionary<string, CartItemInfo> cartItems = new Dictionary<string, CartItemInfo>();

        /// <summary>
        /// Update the quantity for item that exists in the cart
        /// </summary>
        /// <param name="itemId"></param>
        /// <param name="qty"></param>
        public void SetQuantity(string itemId, int qty)
        {
            cartItems[itemId].Quantity = qty;
        }

        /// <summary>
        /// Return the number of items in cart
        /// </summary>
        public int Count
        {
            get
            {
                return this.cartItems.Count;
            }
        }

        /// <summary>
        /// Add a book into the shopping cart
        /// </summary>
        /// <param name="bookISBN">BookISBN</param>
        public void Add(string bookISBN,int qty)
        {
            CartItemInfo cartItem;
            if (!cartItems.TryGetValue(bookISBN, out cartItem))
            {
                Book book = new Book();
                BookInfo bookInfo = book.GetBook(bookISBN);
                if (bookInfo != null)
                {
                    if (qty < bookInfo.BookRemainNum)
                    {
                        CartItemInfo newItem = new CartItemInfo(bookInfo.ISBN, bookInfo.BookName, bookInfo.BookPrice, qty);
                        cartItems.Add(bookISBN, newItem);
                    }
                    else
                    {
                        throw new ApplicationException("Not Enough book");
                    }
                }
            }
            else
            {
                cartItem.Quantity += qty;
            }
            
        }

        /// <summary>
        /// Add a cart item into the shopping cart
        /// </summary>
        /// <param name="book">Cart item</param>
        public void Add(CartItemInfo item)
        {
            CartItemInfo cartItem;
            if (!cartItems.TryGetValue(item.ItemISBN, out cartItem))
            {
                cartItems.Add(item.ItemISBN, item);
            }
            else
            {
                cartItem.Quantity++;
            }
        }

        /// <summary>
        /// Remove a book 
        /// </summary>
        /// <param name="bookISBN">BookISBN</param>
        public void Remove(string bookISBN)
        {
            this.cartItems.Remove(bookISBN);
        }

        /// <summary>
        /// Get all shopping cart items
        /// </summary>
        /// <returns>Collection of shopping cart items</returns>
        public ICollection<CartItemInfo> GetAllCartItems()
        {
            return cartItems.Values;
        }

        /// <summary>
        /// Convert shopping cart items into order line items
        /// </summary>
        /// <returns>Collection of order line items</returns>
        public IList<LineItemInfo> GetOrderLineItems()
        {
            IList<LineItemInfo> lineItems = new List<LineItemInfo>();
            foreach (CartItemInfo cartItem in cartItems.Values)
            {
                LineItemInfo newItem = new LineItemInfo(cartItem.ItemISBN, cartItem.ItemName, cartItem.UnitPrice, cartItem.Quantity);
                lineItems.Add(newItem);
            }
            return lineItems;
        }

        /// <summary>
        /// Clear the shopping cart
        /// </summary>
        public void Clear()
        {
            cartItems.Clear();
        }

    }
}
